<template>
  <div class="login-container">
    <div class="outside">
      <div class="input-box">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model.trim="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input
              type="password"
              v-model.trim="ruleForm.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <!-- <el-form-item label="确认密码" prop="checkPass">
            <el-input
              type="password"
              v-model="ruleForm.checkPass"
              autocomplete="off"
            ></el-input>
          </el-form-item> -->

          <el-form-item>
            <el-button
              type="primary"
              @click="
                onSubmit()
                eventBus()
              "
              >登录</el-button
            >
            <el-button @click="reSet()">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'))
      } else {
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        pass: '',
        checkPass: '',
        username: ''
      },
      rules: {
        pass: [{ validator: validatePass, trigger: 'blur' }],
        checkPass: [{ validator: validatePass2, trigger: 'blur' }],
        username: [{ required: true, validator: checkAge, trigger: 'blur' }]
      }
    }
  },
  methods: {
    async onSubmit() {
      const { data: res } = await this.$http.post('/api/login', {
        name: this.ruleForm.username,
        pass: this.ruleForm.pass
      })
      if (res.token) {
        localStorage.setItem('token', 'Bearer ' + res.token)
        this.$router.push('/')
        console.log(res.token)
      } else {
        alert(res.message)
      }
      // console.log(res)
    },
    reSet() {
      this.ruleForm.username = ''
      this.ruleForm.pass = ''
      this.ruleForm.checkPass = ''
    },
    // 未生效
    eventBus() {
      this.$EventBus.$emit('trans', this.ruleForm.username)
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  width: 100%;
  height: 100%;
  padding-top: 10px;
  background-color: #7e868e;
  position: fixed;
}
.input-box {
  height: 214px;
  width: 300px;
  margin: 0 90px;
  padding-top: 50px;
}
.outside {
  width: 500px;
  height: 320px;
  margin: 100px auto;
  border-radius: 10px;
  background-color: #fff;
}
</style>
